package com.yzt.zhuju.application;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.yzt.zhuju.R;
import com.yzt.p_base.utils.LogDataUtils;
import com.yzt.plugin_widget.AppTitleBar;

/**
 * author : bob
 * date   : 2022/11/29
 * desc   :
 */
public abstract class BaseFra extends Fragment implements View.OnClickListener {

    protected LinearLayout mNormalLayout;

    protected AppTitleBar appTitleBar;

    protected View rootView;

    protected LayoutInflater mInflater;

    protected FragmentActivity mActivity;

    protected View mLayoutPage;

    protected View mLayoutLoading;

    protected View mLayoutError;

    protected View mLayoutEmpty;

    protected ImageView ivLoadingAnim;

    protected TextView tvLoading, tvPageEmpty, tvPageError;
    protected AnimationDrawable animationDrawable;
    protected LinearLayout mLlBaseRoot;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mInflater = inflater;
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.base_act, null);
        }
        rootView.setTag(getPositionTag());
        if (getActivity() != null) {
            mActivity = getActivity();
        }
        initBaseData();
        initView();
        return rootView;
    }

    private void initBaseData() {
        mLlBaseRoot = findViewById(R.id.ll_base_root);
        mNormalLayout = findViewById(R.id.ll_normalView);
        appTitleBar = findViewById(R.id.tb_title);
        appTitleBar.setLeftBtnListener(v -> mActivity.finish());
        mLayoutPage = findViewById(R.id.layout_status_page);
        mLayoutLoading = findViewById(R.id.layout_loading);
        mLayoutError = findViewById(R.id.layout_error);
        mLayoutEmpty = findViewById(R.id.layout_empty);
        ivLoadingAnim = findViewById(R.id.iv_loading_anim);
        tvLoading = findViewById(R.id.tv_loading);
        tvPageEmpty = findViewById(R.id.tv_page_empty);
        tvPageError = findViewById(R.id.tv_page_error);
        findViewById(R.id.ll_load_fail).setOnClickListener(v -> onRetryRequest());
    }

    public final <E extends View> E findViewById(int id) {
        try {
            return rootView.findViewById(id);
        } catch (ClassCastException ex) {
            LogDataUtils.e("Could not cast View to concrete class.", ex);
            throw ex;
        }
    }

    public void setContentView(int resourceId) {
        mNormalLayout.removeAllViews();
        mInflater.inflate(resourceId, mNormalLayout);
    }

    public abstract void initView();

    public abstract int getPositionTag();

    @Override
    public void onClick(View view) {
        onClick(view.getId());
    }

    protected void onClick(int clickID) {

    }

    protected void onRetryRequest() {

    }

    /**
     * 显示空数据页面
     */
    protected void showEmptyView(Object msg) {
        setDefaultStatus();
        mLayoutEmpty.setVisibility(View.VISIBLE);
        if (msg instanceof String) {
            tvPageEmpty.setText((String) msg);
        } else if (msg instanceof Integer) {
            tvPageEmpty.setText((Integer) msg);
        }
    }

    /**
     * 显示空数据页面
     */
    protected void showEmptyView() {
        setDefaultStatus();
        mLayoutEmpty.setVisibility(View.VISIBLE);
    }

    /**
     * 显示加载错误界面
     */
    protected void showErrorView(Object msg) {
        setDefaultStatus();
        mLayoutError.setVisibility(View.VISIBLE);
        if (msg instanceof String) {
            tvPageError.setText((String) msg);
        } else if (msg instanceof Integer) {
            tvPageError.setText((Integer) msg);
        }
    }

    /**
     * 显示加载错误界面
     */
    protected void showErrorView() {
        setDefaultStatus();
        mLayoutError.setVisibility(View.VISIBLE);
    }

    /**
     * 正常页面
     */
    protected void showNormalView() {
        mLayoutPage.setVisibility(View.GONE);
        mNormalLayout.setVisibility(View.VISIBLE);
        if (animationDrawable != null) {
            animationDrawable.stop();
        }
    }

    /**
     * 加载中
     */
    protected void showLoadingView() {
        setDefaultStatus();
        mLayoutLoading.setVisibility(View.VISIBLE);
        animationDrawable = (AnimationDrawable) ivLoadingAnim.getBackground();
        if (animationDrawable != null) {
            animationDrawable.start();
        }
    }

    private void setDefaultStatus() {
        mLayoutPage.setVisibility(View.VISIBLE);
        mNormalLayout.setVisibility(View.GONE);
        mLayoutEmpty.setVisibility(View.GONE);
        mLayoutError.setVisibility(View.GONE);
        mLayoutLoading.setVisibility(View.GONE);
    }

    public void hideLayoutPage() {
        showNormalView();
    }
}
